package cn.net.cfss.fgbp.provider.console.spi;

import cn.net.cfss.fgbp.api.console.dto.SysRoleAppDto;
import cn.net.cfss.fgbp.api.console.inte.ISysRoleAppService;
import cn.net.cfss.fgbp.api.console.vo.SysRoleAppVo;
import cn.net.cfss.fgbp.provider.console.biz.SysRoleAppDtoBiz;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author oygq
 * @title: ISysRoleAppServiceImpl
 * @description: TODO
 * @date 2019/4/8 15:50
 */
@RestController
@Slf4j
public class SysRoleAppSpi implements ISysRoleAppService {

    @Autowired
    SysRoleAppDtoBiz roleAppDtoBiz;

    @Override
    public boolean insert(SysRoleAppVo vo) {
        try {
            //roleAppDtoBiz.insertBatch(vo);
            SysRoleAppDto dto = new SysRoleAppDto();
            dto.setRoleId(vo.getRoleId());
            roleAppDtoBiz.delete(dto);
            roleAppDtoBiz.insertBatch(vo);
        } catch (Exception e) {
            log.error("角色应用新增错误:{}", e.getMessage());
            return false;
        }
        log.info("请求参数:{}，返回参数：{}", vo, true);
        return true;
    }

    @Override
    public boolean update(SysRoleAppVo vo) {
        try {
            SysRoleAppDto dto = new SysRoleAppDto();
            dto.setRoleId(vo.getRoleId());
            roleAppDtoBiz.delete(dto);
            roleAppDtoBiz.insertBatch(vo);
            //roleAppDtoBiz.updateSelectiveById(vo);
        } catch (Exception e) {
            log.error("角色应用修改错误:{}", e.getMessage());
            return false;
        }
        log.info("请求参数:{}，返回参数：{}", vo, true);
        return true;
    }

    @Override
    public boolean delete(Long roleAppId) {
        try {
            roleAppDtoBiz.deleteById(roleAppId);
        } catch (Exception e) {
            log.error("角色应用删除错误:{}", e.getMessage());
            return false;
        }
        log.info("请求参数:{}，返回参数：{}", roleAppId, true);

        return true;
    }

    @Override
    public boolean deleteBatch(Long[] roleAppIds) {
        try {
            roleAppDtoBiz.deleteBatch(roleAppIds);
        } catch (Exception e) {
            log.error("角色应用批量删除错误:{}", e.getMessage());
            return false;
        }

        log.info("请求参数:{}，返回参数：{}", roleAppIds, true);
        return true;
    }

    @Override
    public List<Long> queryByAppId(Long roleId) {
        SysRoleAppVo vo = new SysRoleAppVo();
        vo.setRoleId(roleId);
        List<SysRoleAppDto> roleAppDtos = roleAppDtoBiz.selectList(vo);
        List<Long> apps = new ArrayList<>();
        for(SysRoleAppDto dto : roleAppDtos){
            apps.add(dto.getAppId());
        }

        log.info("请求参数:{}，返回参数：{}", roleId, apps);

        return apps;
    }
}
